﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Principal;

namespace TestJQGrid
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {

        //public DropDownList DdlProductList { get { return ddlProductTypes; } }
        protected void Page_Load(object sender, EventArgs e)
        {
            //set loginId
            var test = WindowsIdentity.GetCurrent().Name.ToString().Split("\\".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            if (test.Count() > 1)
                Session["loginId"] = test[1];
            else
                Session["loginId"] = "default";

            //set menu etc based on login
            if (Session["loggedIn"] == null) Session["loggedIn"] = "false";
            if (Session["loggedIn"].ToString() == "true") loginButton.Text = "Logout";
            if (Session["loggedIn"].ToString() == "false") loginButton.Text = "Login";

            //if user is not logged in make menu invisible
            if ( Session["loggedIn"].ToString() == "false" )
            {
                NavigationMenu.Visible = false;
                loginButton.Enabled = true;
            }
            else if ( Session["loggedIn"].ToString() == "true" )
            {
                ProductTypes.Enabled = false;
                ProductTypes.SelectedValue = Session["ProductName"].ToString();
            }

            if (Session["SelectedReport"] == null)
                Session["SelectedReport"] = 1;
        }

        protected void loginButton_Click(object sender, EventArgs e)
        {
            if ( loginButton.Text == "Login" )
            {
                loginButton.Text = "Logout";
                NavigationMenu.Visible = true;
                ProductTypes.Enabled = false;
                Session["loggedIn"] = "true";
                Session["ProductName"] = ProductTypes.SelectedValue.ToString();
                return;
            }
            if ( loginButton.Text == "Logout" )
            {
                loginButton.Text = "Login";
                NavigationMenu.Visible = false;
                ProductTypes.Enabled = true;
                Session["loggedIn"] = "false";
                Session["ProductName"] = "";
                Response.Redirect("~/Default.aspx");
                return;
            }
            //System.Web.HttpContext.Current.Response.Write(Session["loggedIn"].ToString());
        }
    }
}
